package com.ilop.sthome.common.command;

import android.content.Context;
import android.util.Log;

import com.aliyun.alink.linksdk.tmp.device.panel.PanelDevice;
import com.ilop.sthome.common.udp.GatewayUdpUtil;
import com.ilop.sthome.common.udp.UdpCommunicationUtil;
import com.ilop.sthome.data.greenDao.DeviceInfoBean;

/**
 * @author skygge
 * @Date on 2020-02-25.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：发送设备相关指令
 */
public class SendDeviceCommand {
    private static final String TAG = SendDeviceCommand.class.getSimpleName();
    private Context mContext;
    private SiterCommand mSend;
    private String mIotId;
    private String mDeviceName;

    public SendDeviceCommand(Context context, DeviceInfoBean device) {
        this.mContext = context;
        this.mIotId = device.getIotId();
        this.mDeviceName = device.getDeviceName();
        mSend = new SiterCommand(device);
    }

    /**
     * send code
     * @param groupCode
     */
    private void sendAction(String groupCode){
        if (GatewayUdpUtil.getInstance().isGatewayIntranet(mDeviceName)){
            Log.i(TAG, "sendCodeUDP: " + groupCode);
            new UdpCommunicationUtil(mDeviceName, groupCode);
        }else {
            Log.i(TAG, "sendCodeTCP: " + groupCode);
            PanelDevice panelDevice = new PanelDevice(mIotId);
            panelDevice.init(mContext, null);
            panelDevice.invokeService(groupCode, (b, o) -> {});
        }
    }

    /**
     * send control command
     * @param deviceId
     * @param status2
     */
    public void sendEquipmentCommand(int deviceId, String status2){
        sendAction(mSend.equipControl(deviceId, status2));
    }

    /**
     * sendGateWaySilence
     */
    public void sendGateWaySilence(){
        sendAction(mSend.equipControl(0,"00000000"));
    }


    /**
     * increase equipment command
     */
    public void increaseEquipment(String roomId){
        sendAction(mSend.equipIncrease(roomId));
    }

    /**
     * delete equipment
     * @param deviceId
     */
    public void deleteEquipment(int deviceId){
        sendAction(mSend.equipDelete(deviceId));
    }


    /**
     * replace equipment
     * @param deviceId
     */
    public void replaceEquipment(int deviceId, String deviceType){
        sendAction(mSend.equipReplace(deviceId, deviceType));
    }


    /**
     * modify equipment
     * @param deviceId
     */
    public void modifyEquipmentName(int deviceId, String newName){
        sendAction(mSend.modifyEquipmentName(deviceId, newName));
    }


    /**
     * syn get device status
     * @param deviceCRC
     */
    public void synGetDeviceStatus(String deviceCRC){
        sendAction(mSend.synDeviceStatus(deviceCRC));
    }

    /**
     * syn all device status
     * @param deviceCRC
     */
    public void synAllDeviceStatus(String deviceCRC, String zone){
        sendAction(mSend.synAllDeviceStatus(deviceCRC, zone));
    }

    /**
     * syn get device name
     * @param deviceNameCRC
     */
    public void synGetDeviceName(String deviceNameCRC){
        sendAction(mSend.synDeviceName(deviceNameCRC));
    }

    /**
     * Query or set gateway information
     * @param str1
     * @param str2
     */
    public void queryOrSetGatewayInfo(String str1, String str2){
        sendAction(mSend.queryOrSetGatewayInfo(str1, str2));
    }

    /**
     * modify the SubDevice Room
     * @param deviceId
     * @param roomId
     */
    public void modifySubDeviceRoom(int deviceId, String roomId){
        sendAction(mSend.modifySubDeviceRoom(deviceId, roomId));
    }

    /**
     * cancel increase equipment
     */
    public void cancelIncreaseEq(){
        sendAction(mSend.cancelEquipIncrease());
    }
}
